package com.liy.redisson.service.impl;

import com.liy.redisson.entity.Product;
import com.liy.redisson.mapper.ProductMapper;
import com.liy.redisson.service.OrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author ly
 * @date 2022/11/7 14:52
 */

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private ProductMapper productMapper;

    @Override
    public String addOrder(Long id, String uid) throws InterruptedException {

        String lockKey = id + uid + "ceShi";
        RLock lock = redissonClient.getLock(lockKey);

        try {

            boolean b = lock.tryLock();

//            for (int i = 0; i < 1000; i++) {
//                System.out.println(i);
//                Thread.sleep(3000);
//            }

            return "获取成功";
        }catch (Exception e){
            return "获取超时";
        }finally {
            System.out.println("你好");
            lock.unlock();
        }

    }


    @Override
    public void update(Product product) {
        productMapper.updateById(product);
    }

}
